/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.util.Collection;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import models.Messages;
import models.Users;
import services.Impl.MessageServiceImpl;

/**
 *
 * @author amega
 */
@ManagedBean(name="messageBean")
@SessionScoped
public class MessageBean {
    
    public MessageBean() {
    }
    
    @EJB
    private MessageServiceImpl messageService;
    
    private Messages curentMessage;
    
    private Users userTo;
    
    private Users usersFrom;
    
    private String text;
    
    private String userText;
    
    private Collection<Messages> dialogMessage;

    public Messages getCurentMessage() {
        return curentMessage;
    }

    public void setCurentMessage(Messages curentMessage) {
        this.curentMessage = curentMessage;
    }

    public Collection<Messages> getDialogMessage() {
        return dialogMessage;
    }

    public void setDialogMessage(Collection<Messages> dialogMessage) {
        this.dialogMessage = dialogMessage;
    }
    
    public String getUserText() {
        return userText;
    }

    public void setUserText(String userText) {
        this.userText = userText;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Users getUserTo() {
        return userTo;
    }

    public void setUserTo(Users userTo) {
        this.userTo = userTo;
    }

    public Users getUsersFrom() {
        return usersFrom;
    }

    public void setUsersFrom(Users usersFrom) {
        this.usersFrom = usersFrom;
    }
    
    public void attrListener(ActionEvent event) {
        this.userTo =(Users)event.getComponent().getAttributes().get("userTo");
        this.usersFrom =(Users)event.getComponent().getAttributes().get("userFrom");
    } 
    
    
    public String sendMessage() {

        
        messageService.sendMessageToUser(usersFrom, userTo,text );
        return "";
    }
    
    public void  curMessList(ActionEvent event) {
        curentMessage = (Messages)event.getComponent().getAttributes().get("curentMess");
        curentMessage.setIsRead(Boolean.TRUE);
        messageService.edit(curentMessage);
    }
    
    public String viewDialog(){
        this.dialogMessage = messageService.getUsersDialog(curentMessage.getIdUserTo(),curentMessage.getIdUserFrom() );
        return "userDialog";
    }
    
    public void deleteMess(ActionEvent event){
        Messages mess = (Messages)event.getComponent().getAttributes().get("message");
        mess.getIdUserTo().getMessagesCollectionInput().remove(mess);
        messageService.remove(mess);
        
    }
    
    
}
